﻿using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FireFox
{
    public class Browser : IWebDriver
    {
        private static Browser _instance;

        private Browser(string WebdriverFolderPath)
        {

        }

        public static Browser GetInstance()
        {
            if (_instance == null)
            {
                _instance = CreateNewInstance();
            }

            return _instance;
        }

        private static Browser CreateNewInstance()
        {
            string folderPath = string.Empty;
            if (ConfigurationManager.AppSettings.AllKeys.Contains("ChromeDriverFolderPath"))
            {
                if (ConfigurationManager.AppSettings["ChromeDriverFolderPath"] != null)
                {
                    folderPath = Path.Combine(Environment.CurrentDirectory, "../../../", ConfigurationManager.AppSettings["ChromeDriverFolderPath"]);
                }
            }

            if (string.IsNullOrEmpty(folderPath))
            {
                //ToDo: Need to log a warning here
                folderPath = Path.Combine(Environment.CurrentDirectory, "../../../Dependecies/ChromeDriver");
            }

            if (Directory.Exists(folderPath))
            {
                if (!File.Exists(Path.Combine(folderPath, "chromedriver.exe")))
                {
                    throw new Exception("Specified path doesn't contains chromedriver.exe!");
                }
            }
            else
            {
                throw new Exception("Specified path for chrome driver doesn't exits!");
            }

            return new Browser(folderPath);
        }
    }
}
